class InitAreaIdInUserToolSettings < ActiveRecord::Migration
  def self.up
    # city_id => area_id
    UserToolSetting::WeatherForecast.all.each do |setting|      
      if setting.city.present?
        puts "===========#{ setting.city.areas_count}"
        if setting.city.areas_count > 0
          puts "city: #{setting.city.id}"          
          setting.update_attributes!(:area_id => setting.city.city_area.id) 
        else
          puts "city: #{setting.city.id} hasnt any areas"          
        end          
      else
        puts "city_id: #{setting.city.id} is not exists"
      end
    end if not Rails.env.test?
    provinces = Province.all
    puts "provinces #{provinces.size} "
    provinces.each do |province|
      puts "province #{province.id} areas #{Area.find_all_by_province_id(province.id).size}"
      province.update_attribute :areas_count, Area.find_all_by_province_id(province.id).size
    end if not Rails.env.test?
  end

  def self.down
  end
end
